package myJs.cps

import japgolly.scalajs.react._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.myPkg.jquery._
import org.scalajs.dom.html
import myJs.myPkg.Hooks._

import scala.collection.immutable.SeqMap
import scala.scalajs.js

/** Created by yz on 15/9/2023
  */
object PureTextareaCp {

  case class Props(
      name: String = "",
      hasBv: Boolean = false,
      formJq: JQuery = $("#form"),
      placeHolder: String = "",
      value: js.UndefOr[String] = js.undefined,
      onChange: String => Callback,
      selfClassName: String = "",
      rows: Int = 3
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val inputRef = Ref[html.TextArea]

    val (myV, setMyV) = useState("")

    def valueChange(e: ReactEventFromInput) =  {
      val v = e.target.value
      props.onChange(v)
    }

    val v = props.value.map { x => x }.getOrElse(myV)
    textarea(
      width := "100%",
      className := s"form-control fillByName ${props.selfClassName}",
      name := s"${props.name}",
      value := s"${v}",
      placeholder := props.placeHolder,
      onChange ==> valueChange,
      rows := props.rows
    ).withRef(inputRef)

  }

}
